//
//  ZYCStudentLeaveBottomView.m
//  ZDSC
//
//  Created by ZDTX on 2021/7/26.
//

#import "ZYCStudentLeaveBottomView.h"
#import "LxGridViewFlowLayout.h"
#import "TZTestCell.h"

#define BTImageTotalNumber 9
#define ZYCContentPlaceholder @"请输入您的请假说明~"
#define ZYCContentPlaceholderColor [UIColor colorWithHexString:@"#C3C3C3" alpha:1.0]

@interface ZYCStudentLeaveBottomView ()<UITextViewDelegate,UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIView *line;
@property (nonatomic, strong) UITextView *contentTextView;
@property (nonatomic, strong) UICollectionView * collectionView;
@property (nonatomic, strong) LxGridViewFlowLayout * layout;


@end
@implementation ZYCStudentLeaveBottomView

#pragma mark - setters

-(void) setSelectedPhotos:(NSMutableArray *)selectedPhotos{
    _selectedPhotos = selectedPhotos;
    [self.collectionView reloadData];
}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.titleLabel];
        [self addSubview:self.line];
        [self addSubview:self.contentTextView];
        [self addSubview:self.collectionView];
        
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.equalTo(self).offset(15.5f);
        }];
        
        [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(15.5f);
            make.right.equalTo(self).offset(-15.5f);
            make.top.equalTo(self.titleLabel.mas_bottom).offset(15.0f);
            make.height.mas_equalTo(1);
        }];
        
        [self.contentTextView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.line.mas_bottom).offset(25.0f);
            make.left.equalTo(self).offset(15.5f);
            make.right.equalTo(self).offset(-15.5f);
            make.height.mas_equalTo(120.f);
        }];
        
        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentTextView.mas_bottom).offset(10.0f);
            make.left.equalTo(self).offset(15.5f);
            make.right.equalTo(self).offset(-15.5f);
            make.bottom.equalTo(self).offset(-15.5f);
        }];
    }
    return self;
}

#pragma mark UICollectionViewDelegate && UICollectionViewDataSource
-(NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (self.selectedPhotos.count == BTImageTotalNumber) {
        return self.selectedPhotos.count;
    }else{
        return self.selectedPhotos.count + 1;
    }
}

-(__kindof UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    TZTestCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([TZTestCell class]) forIndexPath:indexPath];
    cell.videoImageView.hidden = YES;
    cell.gifLable.hidden = YES;
    
    if (indexPath.item == self.selectedPhotos.count) {
        cell.imageView.image = [UIImage imageNamed:@"x"];
        cell.deleteBtn.hidden = YES;
    }else {
        cell.imageView.image = self.selectedPhotos[indexPath.row];
        cell.asset = self.selectedAssets[indexPath.item];
        cell.deleteBtn.hidden = NO;
    }
    cell.deleteBtn.tag = indexPath.item;
    [cell.deleteBtn addTarget:self action:@selector(deleteBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    return cell;
}

-(void) collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.item == self.selectedPhotos.count) {
        if ([self.delegate respondsToSelector:@selector(studentLeaveBottomViewDidClickAddPhoto)]) {
            [self.delegate studentLeaveBottomViewDidClickAddPhoto];
        }
    }else{
        [self browserPhotoIndexPath:indexPath];
    }
}

- (void)browserPhotoIndexPath:(NSIndexPath *)indexPath{
    NSMutableArray *browserDataArr = [NSMutableArray array];
    NSUInteger index = indexPath.item;
    [self.selectedAssets enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        YBIBImageData *data = [YBIBImageData new];
        data.imagePHAsset = obj;
        [browserDataArr addObject:data];
    }];
    YBImageBrowser *browser = [YBImageBrowser new];
    browser.dataSourceArray = browserDataArr;
    browser.currentPage = index;
    [browser show];
}

-(void) deleteBtnClick:(UIButton *)sender{
    [_selectedPhotos removeObjectAtIndex:sender.tag];
    [_selectedAssets removeObjectAtIndex:sender.tag];
    [self.collectionView reloadData];
}


#pragma mark - UITextViewDelegate
-(void) textViewDidEndEditing:(UITextView *)textView{
    if (textView.text.length < 1) {
        textView.text = ZYCContentPlaceholder;
        textView.textColor = ZYCContentPlaceholderColor;
    }
}

-(void) textViewDidBeginEditing:(UITextView *)textView{
    if ([textView.text isEqualToString:ZYCContentPlaceholder]) {
        textView.text = @"";
        textView.textColor = [UIColor blackColor];
    }
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    //判断加上输入的字符，是否超过界限
    NSString *string = [NSString stringWithFormat:@"%@%@", textView.text, text];
    if (string.length > 100){
        textView.text = [textView.text substringToIndex:100];
        return NO;
    }
    return YES;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [UILabel LabelWithString:@"说明:" Font:kCustomFont(@"PingFangSC-Light", 16.0f) titleColor:HEX_RGB(kTitleThemeColor) textAlignment:ZZTextAlignmentLeft];
    }
    return _titleLabel;
}
- (UIView *)line {
    if (!_line) {
        _line = [[UIView alloc] init];
        _line.backgroundColor = HEX_RGB(@"F9F9F9");
    }
    return _line;
}

- (UITextView *)contentTextView {
    if (!_contentTextView) {
        _contentTextView = [[UITextView alloc] init];
        _contentTextView.delegate = self;
        _contentTextView.font = kCustomFont(@"PingFangSC-Regular", 16.0f);
        _contentTextView.text = ZYCContentPlaceholder;
        _contentTextView.textColor = ZYCContentPlaceholderColor;
    }
    return _contentTextView;
}

-(UICollectionView *) collectionView
{
    if (!_collectionView) {
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.layout];
        _collectionView.alwaysBounceVertical = YES;
        _collectionView.contentInset = UIEdgeInsetsMake(10, 0, 10, 0);
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.scrollEnabled = NO;
        _collectionView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
        _collectionView.backgroundColor = [UIColor clearColor];
        [_collectionView registerClass:[TZTestCell class] forCellWithReuseIdentifier:NSStringFromClass([TZTestCell class])];
        
    }
    return _collectionView;
}

-(LxGridViewFlowLayout *) layout{
    if (!_layout) {
        _layout = [[LxGridViewFlowLayout alloc] init];
        _layout.itemSize = CGSizeMake(98.0, 98.0f);
        _layout.minimumLineSpacing = 0;//行间距
        _layout.minimumInteritemSpacing = 0;//列间距
        _layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    }
    return _layout;
}


- (NSString *)leaveContent{
    if ([self.contentTextView.text isEqualToString:ZYCContentPlaceholder]) {
        return nil;
    }
    return self.contentTextView.text;
}

#pragma mark - LxGridViewDataSource

/// 以下三个方法为长按排序相关代码
- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath {
    return indexPath.item < _selectedPhotos.count;
}

- (BOOL)collectionView:(UICollectionView *)collectionView itemAtIndexPath:(NSIndexPath *)sourceIndexPath canMoveToIndexPath:(NSIndexPath *)destinationIndexPath {
    return (sourceIndexPath.item < _selectedPhotos.count && destinationIndexPath.item < _selectedPhotos.count);
}

- (void)collectionView:(UICollectionView *)collectionView itemAtIndexPath:(NSIndexPath *)sourceIndexPath didMoveToIndexPath:(NSIndexPath *)destinationIndexPath {
    UIImage *image = _selectedPhotos[sourceIndexPath.item];
    [_selectedPhotos removeObjectAtIndex:sourceIndexPath.item];
    [_selectedPhotos insertObject:image atIndex:destinationIndexPath.item];
    
    id asset = _selectedAssets[sourceIndexPath.item];
    [_selectedAssets removeObjectAtIndex:sourceIndexPath.item];
    [_selectedAssets insertObject:asset atIndex:destinationIndexPath.item];
    
    [_collectionView reloadData];
}
@end
